﻿<template>
  <div class="goods">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
     <el-card>
       <!-- 输入框和按钮区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容"
          v-model="queryInfo.query"
          clearable
          @clear="getGoodsList">
            <el-button
            slot="append"
            icon="el-icon-search"
            @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary"
          @click="goAddPage"
          >添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table
      :data="goodTableList"
      stripe
      border
      style="width: 100%">
        <el-table-column
          type="index"
          label="#">
        </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称">
        </el-table-column>
        <el-table-column
          prop="goods_price"
          width="120px"
          label="商品价格（元）">
        </el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="80px"
          >
        </el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间"
          width="160px">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column
          width="130px"
          label="操作">
          <template slot-scope="scope">
          <el-button type="primary"
          size="mini"
          icon="el-icon-edit"
          @click="editById(scope.row.goods_id)"
          >
          </el-button>
          <!-- @click="removeById(scope.row)" === i -->
          <el-button type="danger"
          icon="el-icon-delete"
          size="mini"
          @click="removeById(scope.row.goods_id)"
          >
          </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
      >
      </el-pagination>
    </el-card>
    <!-- 编辑弹框 -->
     <el-dialog
      title="修改商品"
      :visible.sync="editDialogVisible"
      @close="editDialogClosed"
      width="50%">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input type="number" v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input type="number" v-model="editForm.goods_weight"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" prop="add_time">
          <el-date-picker
            v-model="editForm.add_time"
            type="datetime"
            placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEditForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 查询参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 获取的商品列表数据
      goodTableList: [],
      // 获取的数据总条数
      total: 0,
      // 修改商品信息
      editDialogVisible: false,
      // 修改商品信息表单
      editForm: [],
      editFormRules: {
        goods_name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' }
        ],
        goods_price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' }
        ],
        goods_weight: [
          { required: true, message: '请输入商品重量', trigger: 'blur' }
        ],
        add_time: [
          { required: true, message: '请选择创建时间', trigger: 'blur' }
        ]
      }

    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败!')
      }
      // console.log(res)
      this.goodTableList = res.data.goods
      this.total = res.data.total
      this.$message.success('获取商品列表成功!')
    },
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getGoodsList()
    },
    // 根据id删除商品
    // async removeById (row) {
    async removeById (id) {
      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除商品！')
      }
      // const { data: res } = await this.$http.delete('goods/' + row.goods_id)
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败！')
      }
      this.$message.success('删除商品成功!')
      this.getGoodsList()
    },
    // 点击按钮跳转路由
    goAddPage () {
      this.$router.push('/goods/add')
    },
    async editById (id) {
      // const { data: res } = await this.$http.get(`goods/${id}`)
      // if (res.meta.status !== 200) {
      // return this.$message.error('获取商品信息失败！')
      // }
      // this.editForm = res.data.goods
      // this.$message.success('获取商品信息商品成功!')
      this.editDialogVisible = true
    },
    // 提交修改商品信息时间
    saveEditForm () {
      this.$refs.editFormRef.validate(async valid => {
        if(!valid) return false
        // const { data:res } = await this.$http.put('goods/', this.editForm.id, {
        // goods_price: this.editForm.goods_price,
        // goods_weight: this.editForm.goods_weight,
        // add_time: this.editForm.add_time
        // })
        // if (res.meta.status !== 200) {
        // this.$message.error('更新商品信息失败！')
        // }
        // // 隐藏添加用户对话框
        // this.editDialogVisible = false
        // // 重新获取用户列表数据
        // this.getGoodsList()
        // // 提示修改成功
        // this.$message.success('更新商品信息成功！')
      })
    },
    // 监听弹框关闭并且重置表单
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
      this.editDialogVisible = false
    }
  }
}
</script>

<style lang="less" scoped>
.goods{
  .el-row{
    margin:15px 0;
  }
  .el-date-editor{
    width:100% !important;
  }
}
</style>
